/**
 * Jason M. Burton
 * July, 2012
 * Parser Class
 * Contains an algorithm to parse Java files
 * and create UML diagram based objects
 * Version .3
 * Modded by Shane Burroughs
 */
package umlteam.src.main.java.edu.umuc.duchon.umlteam.main.parsers;

import java.util.Scanner;
import java.io.*;
import java.util.*;

public class Parser {
	String className;
	String parentClass;// if class is not root class
	String variableName;// for each variable in the class
	String variableType;// for each variable type
	String string1;
	String string2;
	String string3;
	String bracket;
	int count;
	File fileName;

	ArrayList<String> variableList = new java.util.ArrayList<String>();
	// the variableList will contain both varibleName and variableType
	
	ArrayList<Method> methodList = new java.util.ArrayList<Method>();//

	
	public Parser(File fileName) {

		this.fileName = fileName;

	}

	public void parseFile(){

		Scanner input;
		try {
			input = new Scanner(fileName);


			input.useDelimiter("[; ,]");

			while (input.hasNext()){//First loop parses through until it finds the class name
				string1 = input.next();   
				if(string1.equals("class")){   
					className = input.next(); 
					string1 = input.next();
					if(string1.equals("extends")){
						parentClass = input.next();
					}                
					break;        
				}      
			}//end while loop
			bracket = input.next();

			//parseClassVarible algorithm
			while (input.hasNext()){
				string1 = input.next();
				variableList.add(string1);
				if(string1.contains("(")){
					string1 =  variableList.remove(variableList.size()-1);
					string2 = variableList.remove(variableList.size()-1);
					string3 = variableList.remove(variableList.size()-1);
					break;
				}       
			}//end while

			//Assign varibleType and variableName then call and parse MethodVariable()
			Method method1 = new Method();
			method1.setMethodReturnType(string3);
			method1.setMethodName(string2);
			method1.addMethodVariable(string1);
			methodList.add(method1);

			//parseMethodVariable()
			while (input.hasNext()){
				string1 = input.next();
				method1.addMethodVariable(string1);
				if(string1.contains(")")){
					// System.out.println("here");
					break;
				}   
			}  

			//Parse through each method and exclude local variables and methods
			while (input.hasNext()){
				string1 = input.next();
				if(string1.contains("{")){
					count++;
				}
				if(string1.contains("}")){
					count--;
				}
				if(count == 0){
					break;
				}
			}//end while

			input.reset();
			input.close();
		}catch (Exception e){
			System.out.println(e.getMessage());
		}

		}




		/* return a string representation of this object */
		public String toString() {
			
			String x = "";
			x = x.concat("Class Name: " + className + "\nParent Class: " + parentClass);
			x = x.concat("\n");
			
			for (int i = 0; i < variableList.size(); i++)
				x = x.concat(variableList.get(i) + "\n");
			
			x = x.concat("\n");
			
			for(int i = 0; i < methodList.size(); i++)
				x = x.concat(variableList.get(i) + "\n");
		
			return x;
			
			

		}// end to String

	}// end Class Parse
